package cn.com.gift.controller;

import cn.com.gift.dto.LoginUserInfo;
import cn.com.gift.dto.LoginUserParam;
import cn.com.gift.dto.PasswordParam;
import cn.com.gift.pojo.SysUser;
import cn.com.gift.service.SysUserService;
import cn.com.gift.utils.GlobalVariable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * Copyright (C), 2018-2024
 * FileName: UserController
 * Author:   Mr
 * Date:     2024/6/11 17:47
 * Description:
 */
@RestController
@RequestMapping("/sys/user")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;

    @PostMapping("/login")
    public Map<String, Object> login (@RequestBody LoginUserParam loginUser, HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();

        if (loginUser == null) {
            result.put("code", 500);
            result.put("message", "请输入登录用户信息！");
            return result;
        }

        if (StringUtils.isEmpty(loginUser.getUserName())) {
            result.put("code", 500);
            result.put("message", "请输入登录用户名！");
            return result;
        }

        if (StringUtils.isEmpty(loginUser.getPassword())) {
            result.put("code", 500);
            result.put("message", "请输入登录用户密码！");
            return result;
        }
        SysUser one = sysUserService.lambdaQuery().eq(SysUser::getUserName, loginUser.getUserName())
                .one();
        if (one == null) {
            result.put("code", 500);
            result.put("message", "当前登录用户不存在！");
            return result;
        }

        String loginPass = GlobalVariable.enPassword(loginUser.getPassword());
        if (!loginPass.equals(one.getPassword())) {
            result.put("code", 500);
            result.put("message", "当前登录用户密码错误，请重试！");
            return result;
        }

        GlobalVariable.putLoginUser(request,
                new LoginUserInfo(one.getUserName(), one.getRealName(),one.getEmail(), LocalDateTime.now()));
        result.put("code", 200);
        result.put("message", "登录成功！");
        return result;
    }

    @PostMapping("/loginInfo")
    public Map<String, Object> info (HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        LoginUserInfo loginUser = GlobalVariable.getLoginUser(request);

        result.put("code", 200);
        result.put("loginUser", loginUser);
        return result;
    }

//    @PostMapping("/password")
//    public Map<String, Object> updatePassword (@RequestBody LoginUserParam loginUserParam, HttpServletRequest request) {
//        Map<String, Object> result = new HashMap<>();
//        if (loginUserParam == null || StringUtils.isEmpty(loginUserParam.getPassword())) {
//            result.put("code", 500);
//            result.put("message", "请输入需要修改的密码！");
//            return result;
//        }
//
//        LoginUserInfo loginUser = GlobalVariable.getLoginUser(request);
//        if (loginUser == null) {
//            result.put("code", 500);
//            result.put("message", "超时登录或未登录，请重新登录！");
//            return result;
//        }
//
//        SysUser one = sysUserService.lambdaQuery().eq(SysUser::getUserName, loginUser.getUserName())
//                .one();
//        if (one == null) {
//            result.put("code", 500);
//            result.put("message", "当前登录用户不存在！");
//            return result;
//        }
//
//        one.setPassword(GlobalVariable.enPassword(loginUserParam.getPassword()));
//        sysUserService.save(one);
//
//        result.put("code", 200);
//        return result;
//    }

    @PostMapping("/password")
    public Map<String, Object> updatePassword (@RequestBody PasswordParam passwordParam, HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        if (passwordParam == null || StringUtils.isEmpty(passwordParam.getOldpwd())) {
            result.put("code", 500);
            result.put("message", "请输入旧密码！");
            return result;
        }

        if ( StringUtils.isEmpty(passwordParam.getNewpwd()) ) {
            result.put("code", 500);
            result.put("message", "请输入新密码！");
            return result;
        }

        if ( StringUtils.isEmpty(passwordParam.getConfirmpwd())) {
            result.put("code", 500);
            result.put("message", "请输入确认密码！");
            return result;
        }

        if ( !passwordParam.getNewpwd().equals(passwordParam.getConfirmpwd())) {
            result.put("code", 500);
            result.put("message", "新密码输入不一致！");
            return result;
        }


        LoginUserInfo loginUser = GlobalVariable.getLoginUser(request);
        if (loginUser == null) {
            result.put("code", 500);
            result.put("message", "超时登录或未登录，请重新登录！");
            return result;
        }

        SysUser one = sysUserService.lambdaQuery().eq(SysUser::getUserName, loginUser.getUserName())
                .one();
        if (one == null) {
            result.put("code", 500);
            result.put("message", "当前登录用户不存在！");
            return result;
        }

        if (!one.getPassword().equals(GlobalVariable.enPassword(passwordParam.getOldpwd()))) {
            result.put("code", 500);
            result.put("message", "旧密码输入错误，请重试！");
            return result;
        }

        one.setPassword(GlobalVariable.enPassword(passwordParam.getNewpwd()));
        sysUserService.saveOrUpdate(one);
        GlobalVariable.removeLoginUser(request);
        result.put("code", 200);
        return result;
    }

    @PostMapping("/logout")
    public Map<String, Object> logout (HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        GlobalVariable.removeLoginUser(request);
        result.put("code", 200);
        return result;
    }
}
